package com.raycloud.erp.omll.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.raycloud.erp.mulplatforms.base.enums.OperateTypeEnum;
import com.raycloud.erp.omll.data.entity.MulPlatformUser;
import com.raycloud.erp.omll.data.entity.OmallSyncInfo;
import com.raycloud.erp.omll.data.entity.dto.MulPlatformUserDTO;
import com.raycloud.erp.omll.data.entity.vo.OmallShopResVO;
import com.raycloud.erp.omll.data.mapper.MulPlatformUserMapper;
import com.raycloud.erp.omll.data.service.MulPlatformUserService;
import com.raycloud.erp.omll.data.service.OmallSyncInfoService;
import com.raycloud.erp.omll.provider.constant.Constants;
import com.raycloud.erp.omll.provider.model.bo.MulPlatformUserBO;
import com.raycloud.erp.omll.provider.service.AccessTokenService;
import com.raycloud.erp.omll.provider.service.PlatformBasisService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 平台店铺信息表 服务实现类
 * </p>
 *
 * @author 枫伦
 * @since 2021-01-05
 */
@Service
public class PlatformBasisServiceImpl implements PlatformBasisService {
    @Autowired
    private MulPlatformUserMapper mulPlatformUserMapper;
    @Autowired
    private AccessTokenService accessTokenService;
    @Autowired
    private MulPlatformUserService mulPlatformUserService;
    @Autowired
    private OmallSyncInfoService omallSyncInfoService;

    @Override
    public List<MulPlatformUser> getAll() {
        return mulPlatformUserService.getAll();
    }

    /***
     * @DESCRIPTION: 新增修改删除一个店铺
     * @author 枫伦
     * @params: [mulPlatformUserBO]
     * @return: java.lang.Boolean
     * @Date: 2021/1/5 6:17 下午
     * @Modified By:
     * @param mulPlatformUserBO
     */
    @Override
    public Boolean registerOrUpdateShop(MulPlatformUserBO mulPlatformUserBO) {
        MulPlatformUser mulPlatformUser = new MulPlatformUser();
        BeanUtils.copyProperties(mulPlatformUserBO, mulPlatformUser);
        mulPlatformUser.setSource(Constants.SOURCE);
        int result = 0;
        if (null == mulPlatformUserBO.getId()) {
            mulPlatformUser.setActive(true);
            result = mulPlatformUserMapper.insert(mulPlatformUser);
            //异步获取店铺的access_token
            if (result > 0) {
                //mybatisplus在执行插入后会吧id的值赋值到mulPlatformUser中
                accessTokenService.getAccessToken(mulPlatformUser);
            }
        } else {
            result = mulPlatformUserMapper.updateById(mulPlatformUser);
        }

        return (result > 0 ? true : false);
    }

    /***
     * @DESCRIPTION: 操作店铺：启用、停用、删除
     * @author 枫伦
     * @params: [mulPlatformUserBO]
     * @return: java.lang.Boolean
     * @Date: 2021/1/13 1:54 下午
     * @Modified By:
     * @param mulPlatformUserBO
     */
    @Override
    @Transactional
    public Boolean operateShop(MulPlatformUserBO mulPlatformUserBO) {
        OperateTypeEnum operateTypeEnum = mulPlatformUserBO.getOperateTypeEnum();
        MulPlatformUser mulPlatformUser = new MulPlatformUser();
        BeanUtils.copyProperties(mulPlatformUserBO, mulPlatformUser);
        if (null == mulPlatformUser.getUserId()) {
            return false;
        }
        int result = 0;
        if (operateTypeEnum.equals(OperateTypeEnum.ACTIVE_SHOP)) {
            //新增
            mulPlatformUser.setActive(true);
            result = mulPlatformUserMapper.updateById(mulPlatformUser);

        } else if (operateTypeEnum.equals(OperateTypeEnum.UNACTIVE_SHOP)) {
            //停用店铺
            mulPlatformUser.setActive(false);
            result = mulPlatformUserMapper.updateById(mulPlatformUser);
            //修改当前店铺对应的拉取订单信息表
            //TODO
            deleteOmallSyncInfo(mulPlatformUser);

        } else if (operateTypeEnum.equals(OperateTypeEnum.DELETE_SHOP)) {
            //删除店铺
            mulPlatformUser.setActive(false);
            mulPlatformUser.setEnableStatus(false);
            result = mulPlatformUserMapper.updateById(mulPlatformUser);
            deleteOmallSyncInfo(mulPlatformUser);

        }
        return (result > 0 ? true : false);
    }

    private void deleteOmallSyncInfo(MulPlatformUser mulPlatformUser) {
        UpdateWrapper<OmallSyncInfo> updateWrapper = new UpdateWrapper();
        updateWrapper.eq("user_id", mulPlatformUser.getUserId());
        updateWrapper.eq("company_id", mulPlatformUser.getCompanyId());
        updateWrapper.set("enable_status", false);
        omallSyncInfoService.update(updateWrapper);
    }

    /**
     * @param mulPlatformUserBO
     * @DESCRIPTION: 获取店铺信息
     * @author 枫伦
     * @params:
     * @return:
     * @Date: 2021/1/13 1:58 下午
     * @Modified By:
     */
    @Override
    public OmallShopResVO getShopInfo(MulPlatformUserBO mulPlatformUserBO) {
        MulPlatformUserDTO mulPlatformUserDTO = new MulPlatformUserDTO();
        BeanUtils.copyProperties(mulPlatformUserBO, mulPlatformUserDTO);
        return mulPlatformUserMapper.getShopInfo(mulPlatformUserDTO);
    }

    /***
     * @DESCRIPTION: 校验token是否过期, 这里无论是否过期都返回true, 如果过期 在这个接口重新刷新token
     * @author 枫伦
     * @params: [mulPlatformUserBO]
     * @return: java.lang.Boolean
     * @Date: 2021/1/13 1:59 下午
     * @Modified By:
     * @param mulPlatformUserBO
     */
    @Override
    public Boolean authCheck(MulPlatformUserBO mulPlatformUserBO) {
        MulPlatformUserDTO mulPlatformUserDTO = new MulPlatformUserDTO();
        BeanUtils.copyProperties(mulPlatformUserBO, mulPlatformUserDTO);
        if (false == mulPlatformUserMapper.authCheck(mulPlatformUserDTO)) {
            //如果token已经过期那么重新刷新
            MulPlatformUser mulPlatformUser = new MulPlatformUser();
            BeanUtils.copyProperties(mulPlatformUserBO, mulPlatformUser);
            accessTokenService.getAccessToken(mulPlatformUser);
        }
        return true;
    }

    /***
     * @DESCRIPTION: 获取到期时间
     * @author 枫伦
     * @params: [mulPlatformUserBO]
     * @return: java.util.Date
     * @Date: 2021/1/13 4:19 下午
     * @Modified By:
     * @param mulPlatformUserBO
     */
    @Override
    public Date getDeadline(MulPlatformUserBO mulPlatformUserBO) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", mulPlatformUserBO.getUserId());
        queryWrapper.eq("company_id", mulPlatformUserBO.getCompanyId());
        queryWrapper.eq("enable_status", true);
        LocalDateTime deadline = mulPlatformUserMapper.selectOne(queryWrapper).getDeadline();
        return Date.from(deadline.atZone(ZoneId.systemDefault()).toInstant());

    }
}
